package com.example.librarysysee.config;

import com.example.librarysysee.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ResponseBody //表示返回的是数据而不是视图（网址）
@ControllerAdvice
public class ErrorHandler {
    /**
     * 捕获异常，并处理
     * @param e
     * @return
     */

    @ExceptionHandler
    public Result handler(Exception e){
        return Result.fail(e.getMessage());
    }
    @ExceptionHandler
    public Result handler(NullPointerException e){
        log.error("捕获异常, e:", e);
        return Result.fail("内部发生NullPointerException错误");
    }

    @ExceptionHandler
    public Result handler(RuntimeException e){
        log.error("捕获异常, e:", e);
        return Result.fail("内部发生RuntimeException错误");
    }

    @ExceptionHandler
    public Result handler(ArithmeticException e){
        log.error("捕获异常, e:", e);
        return Result.fail("内部发生ArithmeticException错误");
    }
}
